import com.asp.aspchain.core.contract.HumanStandardToken_sol_HumanStandardToken;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.gas.ContractGasProvider;
import org.web3j.tx.gas.StaticGasProvider;

import java.math.BigInteger;

public class Contract {
	public static void main(String[] args) throws Exception {
		//erc20 token代币类
		BigInteger GAS_PRICE = BigInteger.valueOf(22_000_000_000L);
		BigInteger GAS_LIMIT = BigInteger.valueOf(4_300_000);
		Web3j web3j = EthServicetest.initWeb3j();
		Credentials credentials = WalletUtils.loadCredentials("", "C:\\Users\\Administrator\\Desktop\\keystore\\UTC--2018-11-09T03-17-03.599782568Z--5bc3081ca939f8646b2fab357564b94d6f791008");
		ContractGasProvider contractGasProvider = new StaticGasProvider(GAS_PRICE, GAS_LIMIT);

		//String account = web3j.ethAccounts().send().getAccounts().get(0);
		/*credentials = Credentials.create("0x5bc3081ca939f8646b2fab357564b94d6f791008");
		ECKeyPair keyPair = credentials.getEcKeyPair();
		BigInteger privateKey = keyPair.getPrivateKey();
		BigInteger publicKey = keyPair.getPublicKey();

		System.out.println(privateKey);
		System.out.println("---");
		System.out.println(publicKey);*/

		/*System.out.println("合约部署外部账号" + credentials.getAddress());
		BigInteger initialAmount = BigInteger.valueOf(1_000_000);
		String tokenName = "DMT";
		BigInteger decimalUnits = BigInteger.valueOf(18);
		String tokenSymbol = "DMT";
		HumanStandardToken_sol_HumanStandardToken contract = HumanStandardToken_sol_HumanStandardToken.deploy(web3j, credentials, contractGasProvider, initialAmount, tokenName, decimalUnits, tokenSymbol).send();

		String contractAddress = contract.getContractAddress();

		System.out.println("合约地址：" + contractAddress);

		System.out.println("合约是否可用：" + contract.isValid());

		System.out.println("合约初始化总发行量：" + contract.totalSupply().send());
		System.out.println("合约拥有者所持有的币：" + contract.balanceOf(credentials.getAddress()).send());
		System.out.println("发行量设置值：" + initialAmount);*/


		String contractAddress1 = "0x812408974349c6d3f3e0322c0d3bc031dad116f9";
		//String contractAddress1 = "0xac8c876b3e6c5509414c4f7b61077798438cb67e";
		HumanStandardToken_sol_HumanStandardToken contract = HumanStandardToken_sol_HumanStandardToken.load(contractAddress1, web3j, credentials, contractGasProvider);
		System.out.println(contract.isValid());

		BigInteger value = BigInteger.valueOf(100L);
		String bob = "0x0000000000000000000000000000000000000111";
		String peter = "0x0000000000000000000000000000000000000222";

		BigInteger bob_b = contract.balanceOf(bob).send();
		BigInteger peter_b = contract.balanceOf(peter).send();
		BigInteger owner = contract.balanceOf("0x5bc3081ca939f8646b2fab357564b94d6f791008").send();
		System.out.println("bob_before:" + bob_b);
		System.out.println("peter_before:" + peter_b);
		System.out.println("owner:" + owner);

		BigInteger bob_allowance = contract.allowance(credentials.getAddress(), bob).send();
		BigInteger peter_allowance = contract.allowance(credentials.getAddress(), peter).send();
		BigInteger unknow_allowance = contract.allowance(credentials.getAddress(), "0x0000000000000000000000000000000000000333").send();
		System.out.println("bob_allowance:" + bob_allowance);
		System.out.println("peter_allowance:" + peter_allowance);
		System.out.println("unknow_allowance:" + unknow_allowance);

		TransactionReceipt send = contract.transfer(bob, BigInteger.valueOf(100L)).send();
		BigInteger bob_n = contract.balanceOf(bob).send();
		System.out.println("bob_now:" + bob_n);

		//先授权，中央银行同意peter转钱的额度
		TransactionReceipt approve = contract.approve(peter, value).send();
		TransactionReceipt approve2 = contract.approve(bob, value).send();
		HumanStandardToken_sol_HumanStandardToken.ApprovalEventResponse approveEventValues =
				contract.getApprovalEvents(approve).get(0);

		System.out.println("交易hash值：" + approve.getTransactionHash());
		System.out.println("同意者：" + approveEventValues._owner);
		System.out.println("被同意者：" + approveEventValues._spender);
		System.out.println("钱：" + approveEventValues._value);
		System.out.println("log：" + approve.getLogs());


		//然后中央银行执行转账，将peter的钱转给bob
		TransactionReceipt tran_receipt = contract.transferFrom(bob, peter, value).send();

		HumanStandardToken_sol_HumanStandardToken.TransferEventResponse transferEventValues =
				contract.getTransferEvents(tran_receipt).get(0);

		System.out.println("交易hash值：" + tran_receipt.getTransactionHash());
		System.out.println("转出方：" + transferEventValues._from);
		System.out.println("转入方：" + transferEventValues._to);
		System.out.println("钱：" + transferEventValues._value);
		System.out.println("log：" + tran_receipt.getLogs());


		BigInteger bob_a = contract.balanceOf(bob).send();
		BigInteger peter_a = contract.balanceOf(peter).send();
		System.out.println("bob_after:" + bob_a);
		System.out.println("peter_after:" + peter_a);
	}


	/*public static void main(String[] args) throws Exception {
		//erc721 资产类
		BigInteger GAS_PRICE = BigInteger.valueOf(22_000_000_000L);
		BigInteger GAS_LIMIT = BigInteger.valueOf(4_300_000);
		Web3j web3j = EthServicetest.initWeb3j();
		Credentials credentials = WalletUtils.loadCredentials("", "C:\\Users\\Administrator\\Desktop\\keystore\\UTC--2018-11-09T03-17-03.599782568Z--5bc3081ca939f8646b2fab357564b94d6f791008");
		ContractGasProvider contractGasProvider = new StaticGasProvider(GAS_PRICE, GAS_LIMIT);
		*//*System.out.println("合约部署外部账号:" + credentials.getAddress());
		ERC721Mintable_sol_ERC721Mintable contract = ERC721Mintable_sol_ERC721Mintable.deploy(web3j, credentials, contractGasProvider).send();

		String contractAddress = contract.getContractAddress();

		System.out.print("合约地址：" + contractAddress);

		System.out.print("合约是否可用：" + contract.isValid());*//*


		String contractAddress1 = "0x57702b951295a1e4a4c60a837b94839a7173956e";
		ERC721Mintable_sol_ERC721Mintable contract = ERC721Mintable_sol_ERC721Mintable.load(contractAddress1, web3j, credentials, contractGasProvider);
		System.out.println(contract.isValid());


		*//*contract.transferEventObservable(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST).subscribe(transferEventResponse -> {
			System.out.println("------监控开始------");
			System.out.println("from:"+transferEventResponse.from);
			System.out.println("to:"+transferEventResponse.to);
			System.out.println("tokenId:"+transferEventResponse.tokenId);
			System.out.println("hashCode:"+transferEventResponse.hashCode());
			System.out.println("------监控结束------");
		});*//*




		String from = "0x5bc3081ca939f8646b2fab357564b94d6f791008";
		String to = "0x0000000000000000000000000000000000000111";
		BigInteger from_b = contract.balanceOf(from).send();
		BigInteger to_b = contract.balanceOf(to).send();
		System.out.println("from_b:" + from_b + ",to_b:" + to_b);
		BigInteger tokenId = BigInteger.valueOf(101012);


		TransactionReceipt receipt = contract.mint(to, tokenId).send();
		ERC721Mintable_sol_ERC721Mintable.TransferEventResponse mintTransferEventValues =
				contract.getTransferEvents(receipt).get(0);
		System.out.println(mintTransferEventValues.log);

		TransactionReceipt receipt2 = contract.safeTransferFrom(from, to, tokenId).send();
		ERC721Mintable_sol_ERC721Mintable.TransferEventResponse transferEventValues =
				contract.getTransferEvents(receipt2).get(0);
		System.out.println(transferEventValues.log);


		// CHECKSTYLE:OFF


		BigInteger from_a = contract.balanceOf(from).send();
		BigInteger to_a = contract.balanceOf(to).send();
		System.out.println("from_a:" + from_a + ",to_a:" + to_a);

	}*/
}
